home *** CD-ROM | disk | FTP | other *** search
/ Aminet 15 / Aminet 15 - Nov 1996.iso / Aminet / dev / c / EasygadgetsSou.lha / EasyGadgets / EasyGadgets.c < prev    next >
C/C++ Source or Header  |  1995-12-03  |  12KB  |  483 lines

  1. /*
  2.  *    File:                    EasyGadgets.c
  3.  *    Description:    Functions to easy create and handle graphic user interface
  4.  *
  5.  *    (C) 1994,1995 Ketil Hunn
  6.  *
  7.  */
  8.  
  9. #define LIBCODE 1
  10.  
  11. #ifndef EASYGADGETS_H
  12. #define    EASYGADGETS_H
  13.  
  14. #define    SETTAG(array,tag,data)    array.ti_Tag=tag;array.ti_Data=data 
  15.  
  16. /*** PRIVATE INCLUDES ****************************************************************/
  17. #include <exec/types.h>
  18. #include <libraries/gadtools.h>
  19. #include <exec/lists.h>
  20. #include <exec/memory.h>
  21. #include <utility/utility.h>
  22. #include <utility/tagitem.h>
  23. #include <utility/hooks.h>
  24. #include <intuition/imageclass.h>
  25. #include <intuition/intuitionbase.h>
  26. #include <intuition/intuition.h>
  27. #include <libraries/amigaguide.h>
  28. #include <libraries/diskfont.h>
  29. #include <workbench/workbench.h>
  30.  
  31. #include <clib/exec_protos.h>
  32. #include <clib/intuition_protos.h>
  33.  
  34. #include <clib/graphics_protos.h>
  35. #include <clib/gadtools_protos.h>
  36. #include <clib/dos_protos.h>
  37. #include <clib/utility_protos.h>
  38. #include <clib/keymap_protos.h>
  39. #include <clib/amigaguide_protos.h>
  40. #include <clib/alib_stdio_protos.h>
  41. #include <clib/alib_protos.h>
  42. #include <clib/diskfont_protos.h>
  43. #include <clib/wb_protos.h>
  44.  
  45. /*** ScreenNotify.library support ***************************************************/
  46. #include <libraries/screennotify.h>
  47. #include <clib/screennotify_protos.h>
  48. #include <pragmas/screennotify_pragmas.h>
  49.  
  50. struct IntuitionBase    *IntuitionBase;
  51. struct GfxBase                *GfxBase;
  52. struct Library                *GadToolsBase,
  53.                                             *DOSBase,
  54.                                             *UtilityBase,
  55.                                             *AmigaGuideBase,
  56.                                             *KeymapBase,
  57.                                             *DiskfontBase,
  58.                                             *WorkbenchBase,
  59.                                             *SysBase,
  60.                                             *ScreenNotifyBase;
  61.  
  62. #include <pragmas/exec_pragmas.h>
  63. #include <pragmas/dos_pragmas.h>
  64. #include <pragmas/intuition_pragmas.h>
  65. #include <pragmas/gadtools_pragmas.h>
  66. #include <pragmas/utility_pragmas.h>
  67. #include <pragmas/graphics_pragmas.h>
  68. #include <pragmas/keymap_pragmas.h>
  69. #include <pragmas/amigaguide_pragmas.h>
  70. #include <pragmas/diskfont_pragmas.h>
  71. #include <pragmas/wb_pragmas.h>
  72. #include <pragmas/locale_pragmas.h>
  73. #include <pragmas/keymap_pragmas.h>
  74.  
  75. #include <stdlib.h>
  76. #include <clib/macros.h>
  77. #include "myinclude:BitMacros.h"
  78.  
  79. /*** DEFINES *************************************************************************/
  80. #define LIBVER    37L
  81. #define    EG_LIB    1
  82.  
  83. #define    EG_Underscorechar        '_'
  84. #define    EG_Underscorestring    "_"
  85.  
  86. #define    every_task                    task=eg->tasklist; task; task=task->nexttask
  87.  
  88. /*** PROTOTYPES **********************************************************************/
  89. __asm BYTE IsNil(register __a0 struct List *list);
  90.  
  91. /*** PRIVATE INCLUDES ****************************************************************/
  92.  
  93. #define USEFAILREQUEST
  94. #ifdef USEFAILREQUEST
  95. LONG FailRequest(UBYTE *format, APTR arg1, ...);
  96. #endif
  97.  
  98. #include "easygadgets_rev.h"
  99. #include "myinclude:BitMacros.h"
  100. #include <libraries/EasyGadgets.h>
  101. #include <clib/EasyGadgets_protos.h>
  102. #include "myinclude:myString.h"
  103. #include "myinclude:CloseWindowSafely.h"
  104. #include "menu.c"
  105. #include "Help.c"
  106. #include "HandleKeys.c"
  107. #include "Gadgets.c"
  108. #include "Windows.c"
  109. #include "Task.c"
  110. #include "Requesters.c"
  111. #include "IconifyButtonClass.c"
  112. #include "GroupFrameClass.c"
  113.  
  114. /*** LIBRARY INIT ********************************************************************/
  115. void __saveds __UserLibCleanup(void)
  116. {
  117.     if(ScreenNotifyBase)
  118.         CloseLibrary(ScreenNotifyBase);
  119.     if(WorkbenchBase)
  120.         CloseLibrary(WorkbenchBase);
  121.     if(DiskfontBase)
  122.         CloseLibrary(DiskfontBase);
  123.     if(KeymapBase)
  124.         CloseLibrary(KeymapBase);
  125.     if(DOSBase)
  126.         CloseLibrary(DOSBase);
  127.     if(GfxBase)
  128.         CloseLibrary((struct Library *)GfxBase);
  129.     if(GadToolsBase)
  130.         CloseLibrary(GadToolsBase);
  131.     if(UtilityBase)
  132.         CloseLibrary(UtilityBase);
  133.     if(IntuitionBase)
  134.         CloseLibrary((struct Library *)IntuitionBase);
  135. }
  136.  
  137. int __saveds __UserLibInit(void)
  138. {
  139.     SysBase=*((void **)4L);
  140.  
  141.     ScreenNotifyBase=OpenLibrary(SCREENNOTIFY_NAME, SCREENNOTIFY_VERSION);
  142.  
  143.     if(IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library", LIBVER))
  144.         if(UtilityBase=OpenLibrary("utility.library", LIBVER))
  145.             if(GadToolsBase=OpenLibrary("gadtools.library", LIBVER))
  146.                 if(GfxBase=(struct GfxBase *)OpenLibrary("graphics.library", LIBVER))
  147.                     if(DOSBase=OpenLibrary("dos.library", LIBVER))
  148.                         if(KeymapBase=OpenLibrary("keymap.library", 0L))
  149.                             if(DiskfontBase=OpenLibrary("diskfont.library", LIBVER))
  150.                                 if(WorkbenchBase=OpenLibrary(WORKBENCH_NAME, LIBVER))
  151.                                     return 0;
  152.     __UserLibCleanup();
  153.     return 1;
  154. }
  155.  
  156. /*** FUNCTIONS ***********************************************************************/
  157.  
  158. #ifdef USEFAILREQUEST
  159. LONG FailRequestA(UBYTE *format, APTR *args)
  160. {
  161.     struct EasyStruct myES;
  162.  
  163.     myES.es_StructSize        =sizeof(struct EasyStruct);
  164.     myES.es_Title                    =NAME " " VERS;
  165.     myES.es_TextFormat        =format;
  166.     myES.es_GadgetFormat    ="OK";
  167.  
  168.     return EasyRequestArgs(NULL, &myES, NULL, args);
  169. }
  170.  
  171. LONG FailRequest(UBYTE *format, APTR arg1, ...)
  172. {
  173.     return FailRequestA(format, &arg1);
  174. }
  175. #endif
  176.  
  177. /*** FUNCTIONS ***********************************************************************/
  178. __asm __saveds WORD egTextWidth(register __a0 struct EasyGadgets    *eg,
  179.                                                                 register __a1 STRPTR                            text)
  180. {
  181.     register WORD        length;
  182.  
  183. #ifdef MYDEBUG_H
  184.     DebugOut("egNewTextWidth");
  185. #endif
  186.  
  187.     length=TextLength(&eg->RPort, text, StrLen(text));
  188.  
  189.     if(StrChr(text, EG_Underscorechar))
  190.         length-=TextLength(&eg->RPort, EG_Underscorestring, 1);
  191.     return length;
  192. }
  193.  
  194. __asm __saveds WORD egMaxLenA(register __a1 struct EasyGadgets    *eg,
  195.                                                             register __a0 UBYTE                                **array)
  196. {
  197.     register WORD        maxlen=0;
  198.     register ULONG    i=0;
  199.     register char        *c;
  200.  
  201. #ifdef MYDEBUG_H
  202.     DebugOut("egMaxLenA");
  203. #endif
  204.  
  205.     while(c=array[i++])
  206.         maxlen=MAX(maxlen, egTextWidth(eg, c));
  207.  
  208.     return maxlen;
  209. }
  210.  
  211. __asm __saveds void egSpreadGadgets(register __a0 WORD *posarray,
  212.                                                                         register __a1 WORD *sizearray,
  213.                                                                         register __d0 WORD x1,
  214.                                                                         register __d1 WORD x2,
  215.                                                                         register __d2 ULONG count,
  216.                                                                         register __d3 BYTE space)
  217. {
  218.     register    WORD    totalsize=0, width=x2-x1, gadspace=0;
  219.     register    ULONG last=count-1, i;
  220.  
  221. #ifdef MYDEBUG_H
  222.     DebugOut("egSpreadGadgets");
  223. #endif
  224.  
  225.     if(count==1)
  226.         posarray[0]=x1+(((x2-x1)-sizearray[0])/2);
  227.     else
  228.     {
  229.         for(i=0; i<count; i++)
  230.             totalsize+=sizearray[i];
  231.         if(space==TRUE)
  232.             gadspace=(width-totalsize)/(count-1);
  233.  
  234.         posarray[0]=x1;
  235.  
  236.         if(count>2)
  237.             for(i=1; i<last; i++)
  238.                 posarray[i]=posarray[i-1]+sizearray[i-1]+gadspace;
  239.  
  240.         posarray[last]=x2-sizearray[last];
  241.     }
  242. }
  243.  
  244. __asm __saveds void egInitialize(    register __a0 struct EasyGadgets    *eg,
  245.                                                                     register __a1 struct Screen                *screen,
  246.                                                                     register __a2 struct TextFont            *font)
  247. {
  248.     register struct Window    *window;
  249.     register BYTE islaced=(BYTE)egIsDisplay(screen, DIPF_IS_LACE);
  250.  
  251. #ifdef MYDEBUG_H
  252.     DebugOut("egInitialize");
  253. #endif
  254.  
  255.     eg->screen    =screen;
  256.     eg->font        =font;
  257.     SetFont(&eg->RPort, font);
  258.  
  259.     eg->FontHeight                =font->tf_YSize+1;
  260.     eg->ScreenBarHeight        =screen->BarHeight+1;
  261.     eg->GadgetKind                =BUTTON_KIND;
  262.  
  263.     eg->VInside                        =eg->HSpace=4;
  264.     eg->HInside                        =15;
  265.  
  266.     eg->GroupBorderLeft        =7;
  267.     eg->GroupBorderRight    =5;
  268.  
  269.     eg->SliderHeight            =MAX(8,                                eg->FontHeight+2);
  270.     eg->SliderWidth                =MAX(16,                            eg->SliderHeight);
  271.  
  272.     if(window=OpenWindowTags(NULL,
  273.                                 WA_Title,                    "Be nice",
  274.                                 WA_Width,                    1,
  275.                                 WA_Height,                1,
  276.                                 WA_CustomScreen,    screen,
  277.                                 WA_SizeGadget,        TRUE,
  278.                                 WA_SizeBBottom,        TRUE,
  279.                                 TAG_END))
  280.     {
  281.         eg->LeftMargin    =4+window->BorderLeft;
  282.         eg->RightMargin    =4+window->BorderRight;
  283.  
  284.         eg->TopMargin            =2+window->BorderTop;
  285.         eg->BottomMargin    =2+window->BorderBottom;
  286.  
  287.         CloseWindow(window);
  288.     }
  289.     if(window=OpenWindowTags(NULL,
  290.                                 WA_Title,                    ":-þ",
  291.                                 WA_Width,                    1,
  292.                                 WA_Height,                1,
  293.                                 WA_CustomScreen,    screen,
  294.                                 TAG_END))
  295.     {
  296.         eg->BottomMarginNoSize=2+window->BorderBottom;
  297.         CloseWindow(window);
  298.     }
  299.  
  300.     if(islaced)
  301.     {
  302.         eg->VSpace                            =4;
  303.         eg->GroupBorderTop            =3+font->tf_YSize/2;
  304.         eg->GroupBorderBottom        =6;
  305.         eg->GroupBorderLeft            =7;
  306.         eg->GroupBorderRight        =7;
  307.  
  308.         eg->TopMargin                        +=2;
  309.         eg->BottomMargin                +=2;
  310.         eg->BottomMarginNoSize    +=2;
  311.     }
  312.     else
  313.     {
  314.         eg->VSpace                            =2;
  315.         eg->GroupBorderTop            =2+font->tf_YSize/2;
  316.         eg->GroupBorderBottom        =5;
  317.         eg->GroupBorderLeft            =10;
  318.         eg->GroupBorderRight        =10;
  319.     }
  320.  
  321.     eg->CheckboxHeight=MAX(CHECKBOX_HEIGHT, eg->FontHeight+2);
  322.     eg->MXHeight            =MAX(MX_HEIGHT,                eg->FontHeight);
  323.  
  324.     if(KickStart>38)
  325.     {
  326.         register WORD    medchar=TextLength(&eg->RPort, "abcdefghijklmnopqrstuvwxyz", 26)/26;
  327.  
  328.         if(islaced && eg->FontHeight>8)
  329.         {
  330.             eg->CheckboxWidth    =eg->CheckboxHeight+2;
  331.             eg->MXWidth                =eg->MXHeight+2;
  332.         }
  333.         else
  334.         {
  335.             eg->CheckboxWidth    =MIN(CHECKBOX_WIDTH,    medchar*3+2);
  336.             eg->MXWidth                =MIN(MX_WIDTH,                medchar*3);
  337.         }
  338.     }
  339.     else
  340.     {
  341.         eg->CheckboxWidth    =CHECKBOX_WIDTH;
  342.         eg->MXWidth                =MX_WIDTH;
  343.     }
  344.  
  345.     eg->DefaultHeight        =eg->FontHeight+eg->VInside;
  346. }
  347.  
  348. __asm void egSafeDeleteMsgPort(register __a0 struct MsgPort *port)
  349. {
  350.     if(port)
  351.     {
  352.         register struct Message *msg;
  353.  
  354.         while(msg=GetMsg(port))
  355.             ReplyMsg(msg);
  356.         DeleteMsgPort(port);
  357.     }
  358. }
  359.  
  360. __asm __saveds void egFreeEasyGadgets(register __a0 struct EasyGadgets *eg)
  361. {
  362. #ifdef MYDEBUG_H
  363.     DebugOut("egFreeEasyGadgets");
  364. #endif
  365.  
  366.     if(eg)
  367.     {
  368.         egCloseAllTasks(eg);
  369.  
  370.         if(eg->iconifyclass)
  371.             FreeClass(eg->iconifyclass);
  372.  
  373.         if(eg->groupframeclass)
  374.             FreeClass(eg->groupframeclass);
  375.  
  376.         if(eg->helpdoc!=NULL)
  377.         {
  378.             egCloseAmigaGuide(eg);
  379.             FreeVec(eg->helpdoc);
  380.         }
  381.         if(eg->basename!=NULL)
  382.             FreeVec(eg->basename);
  383.         eg->AmigaGuideSignal=0L;
  384.         FreeVec(eg->msg);
  385.  
  386.         egSafeDeleteMsgPort(eg->msgport);
  387.  
  388.         if(eg->wbhandle)
  389.             while(!RemWorkbenchClient(eg->wbhandle))
  390.                 Delay(10);
  391.         egSafeDeleteMsgPort(eg->notifyport);
  392.  
  393.         FreeVec(eg);
  394.     }
  395. }
  396.  
  397. __asm __saveds struct EasyGadgets *egAllocEasyGadgetsA(register __a0 struct TagItem *taglist)
  398. {
  399.     register struct EasyGadgets    *eg;
  400.  
  401. #ifdef MYDEBUG_H
  402.     DebugOut("egAllocEasyGadgetsA");
  403. #endif
  404.  
  405.     if(eg=(struct EasyGadgets *)AllocVec(sizeof(struct EasyGadgets), MEMF_CLEAR|MEMF_PUBLIC))
  406.         if(eg->msg=(struct IntuiMessage *)AllocVec(sizeof(struct IntuiMessage), NULL))
  407.             if(eg->msgport=CreateMsgPort())
  408.             {
  409.                 struct TagItem    *tstate=taglist;
  410.                 register struct TagItem    *tag;
  411.  
  412.                 InitRastPort(&eg->RPort);
  413.  
  414.                 eg->iconifyclass=initIconifyButtonClass();
  415.                 eg->groupframeclass=initGroupFrameClass();
  416.  
  417.                 if(ScreenNotifyBase)
  418.                     eg->notifyport=CreateMsgPort();
  419.  
  420.                 while(tag=NextTagItem(&tstate))
  421.                     switch(tag->ti_Tag)
  422.                     {
  423.                         case EG_HelpDocument:
  424.                             eg->helpdoc=StrDup((UBYTE *)tag->ti_Data);
  425.                             break;
  426.                         case EG_Basename:
  427.                             eg->basename=StrDup((UBYTE *)tag->ti_Data);
  428.                             break;
  429.                         case EG_AppIcon:
  430.                             eg->diskobj=(struct DiskObject *)tag->ti_Data;
  431.                             break;
  432.                         case EG_WorkbenchNotify:
  433.                             if(eg->notifyport && tag->ti_Data)
  434.                                 eg->wbhandle=AddWorkbenchClient(eg->notifyport, 0);
  435.                             break;
  436.                     }
  437.             }
  438.     return eg;
  439. }
  440.  
  441. #define    ID_APPICON    1
  442. #define    ID_APPMENU    2
  443.  
  444. __asm __saveds BYTE egIconify(register __a0 struct EasyGadgets    *eg,
  445.                                                             register __d0 BYTE                                doit)
  446. {
  447.     register BYTE success=FALSE;
  448.  
  449.     if(doit)
  450.     {
  451.         if(ISBITCLEARED(eg->flags, EG_ICONIFIED))
  452.         {
  453.             register struct Message *msg;
  454.  
  455.             eg->appicon=NULL;
  456.             if(eg->diskobj)
  457.             {
  458.                 if(eg->appicon=AddAppIconA(    ID_APPICON, 0L,
  459.                                                                         eg->basename,
  460.                                                                         eg->msgport,
  461.                                                                         NULL, eg->diskobj, NULL))
  462.                 {
  463.                     SETBIT(eg->flags, EG_ICONIFIED);
  464.                     egCloseAllTasks(eg);
  465.                     success=TRUE;
  466.                 }
  467.             }
  468.             while(msg=GetMsg(eg->msgport))
  469.                 ReplyMsg(msg);
  470.         }
  471.     }
  472.     else if(eg->appicon)
  473.     {
  474.         egOpenAllTasks(eg);
  475.         RemoveAppIcon(eg->appicon);
  476.         CLEARBIT(eg->flags, EG_ICONIFIED);
  477.         success=TRUE;
  478.     }
  479.     return success;
  480. }
  481.  
  482. #endif
  483.